package com.linwen.excise.leetcode;

/**
 * @ClassName _35搜索插入位置
 * @Description 搜索插入位置
 * @Author lin
 * @Date 2021/11/7 下午1:08
 * @Version V1.0
 */
public class _35搜索插入位置 {
    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5,6};
        int target = 2;
        System.out.println(searchInsert(nums,target));
    }
    public static int searchInsert(int[] nums, int target) {
        int start = 0;
        int end = nums.length-1;
        while (start<=end){
            int mid = start + (end-start)/2;
            if(target>nums[mid]){
                start = mid+1;
            }else if(target<nums[mid]){
                end = mid-1;
            }else {
                return mid;
            }
        }
        return start;
    }
}
